import 'package:escooter_patcher_flutter/base/base.dart';
import 'package:escooter_patcher_flutter/operation_rs/operation_rs_controller.dart';
import 'package:escooter_patcher_flutter/prompt.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dialogs/flutter_dialogs.dart';
import 'package:get/get.dart';

class OperationRSPage extends BaseView<OperationRSController> {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    CupertinoPageScaffold(
      child: Text(""),
    );
    return Scaffold(
      appBar: AppBar(
        title: const Text("Scooter Information"),
        backgroundColor: Colors.black87,
      ),
      body: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
              margin: const EdgeInsets.only(left: 16, right: 16, top: 16),
              child: Obx(() => Column(mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisSize: MainAxisSize.min, children: [
                    _buildInfoItem("SN", controller.sn),
                    _buildInfoItem("Model", controller.currentModel),
                    _buildInfoItem("Firmware Version", controller.version),
                    Container(
                        margin: const EdgeInsets.only(top: 16, bottom: 16),
                        child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                          const Text("Current Region"),
                          MaterialButton(
                              child: Row(
                                children: [
                                  Text(
                                    controller.currentRegion.string,
                                    style: const TextStyle(color: Color(0xFF888888)),
                                  ),
                                  const Icon(
                                    Icons.arrow_drop_down,
                                    size: 20,
                                  )
                                ],
                              ),
                              onPressed: () {
                                _showRegions2ChangeDialog(context, controller);
                              }),
                        ])),
                    Container(
                      margin: const EdgeInsets.only(top: 32),
                      child: MaterialButton(
                        onPressed: () => controller.disconnectCurrent(),
                        color: Colors.black87,
                        textColor: Colors.white,
                        padding: const EdgeInsets.only(top: 12, bottom: 12, left: 20, right: 20),
                        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                        child: const Text(
                          "DISCONNECT",
                        ),
                      ),
                    ),
                  ]))),
          Spacer(),
          Column(
            children: [
              Container(
                margin: const EdgeInsets.only(top: 32),
                child: MaterialButton(
                  onPressed: () => controller.showDisclaimerDialog(),
                  child: const Text("Disclaimer"),
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  _buildInfoItem(String name, Rx value) {
    return Container(
        margin: const EdgeInsets.only(top: 16, bottom: 16),
        child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
          Text(name),
          Text(
            value.string,
            style: const TextStyle(color: Color(0xFF888888)),
          )
        ]));
  }

  _showRegions2ChangeDialog(BuildContext context, OperationRSController controller) {
    showPlatformDialog(
      context: context,
      builder: (context) => BasicDialogAlert(
        title: const Text("Change Scooter's Region"),
        content: Container(height: 200, width: 200, child: ListView(children: _buildRegionsItem(controller, controller.regions))),
        actions: [
          BasicDialogAction(
            title: const Text("Cancel"),
            onPressed: () => Navigator.pop(context),
          ),
        ],
      ),
    );
  }

  _buildRegionsItem(OperationRSController controller, List<String> regions) {
    List<Container> widgets = [];
    for (var i = 0; i < regions.length; i++) {
      widgets.add(Container(
          margin: const EdgeInsets.only(top: 2, bottom: 2),
          child: MaterialButton(
              child: Text(regions[i]),
              onPressed: () {
                if (controller.donNotSupportChangeRegion) {
                  Prompt.showMessageDialog("Don't support current software version!");
                  return;
                }
                Get.back();
                controller.changeVehicleRegion(i);
              })));
    }
    return widgets;
  }
}
